<?php
/**
 * Copyright 2010 Dave Keen
 * http://www.actionscriptdeveloper.co.uk
 * 
 * This file is part of Flextrine.
 * 
 * Flextrine is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Lesser GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * and the Lesser GNU General Public License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */

class FlextrineService extends \Flextrine\AbstractFlextrineService {	
	
	protected $flextrineManagerEnabled = true;

	function __construct() {
		// Database connection information
		$connectionOptions = array(
			'dbname' => 'orm',
			'user' => 'root',
			'password' => '',
			'host' => 'localhost',
			'driver' => 'pdo_mysql',
		);
		
		parent::__construct(dirname(__FILE__), $connectionOptions);
	}
	
	protected function runCustomOperation($operation, $data) {
		switch ($operation) {
			case "dropSchema":
				$this->dropSchema();
				break;
			case "createSchema":
				$this->createSchema();
				break;
			case "createGarden":
				$this->createGarden();
				break;
		}
	}
	
	public function useFixture($fixture) {
		$conn = $this->em->getConnection();
		
		$conn->executeUpdate("SET FOREIGN_KEY_CHECKS=0");
		
		foreach ($conn->getSchemaManager()->listTableNames() as $tableName) {
			$conn->executeUpdate("ALTER TABLE $tableName DISABLE KEYS");
			$conn->executeUpdate("DELETE FROM $tableName");
			$conn->executeUpdate("ALTER TABLE $tableName AUTO_INCREMENT = 1");
		}
		
		if ($fixture)
			$conn->exec(file_get_contents(dirname(__FILE__)."/fixtures/$fixture.sql"));
			
		$conn->executeUpdate("SET FOREIGN_KEY_CHECKS=1");
		
		return true;
	}
	
	protected function getSchemaEntities() {
		return parent::getSchemaEntities(dirname(__FILE__)."/../entities");
	}
	
	public function getEntityManager() {
		return $this->em;
	}
	
}